package com.gee.spring.cloud.aerospace.simulate.mgt.controller;


import com.gee.cloud.common.core.api.domain.result.ApiResult;
import com.gee.spring.cloud.aerospace.simulate.core.satellite.telemetry.common.SateTraceContext;
import com.gee.spring.cloud.aerospace.simulate.core.satellite.telemetry.common.SatelliteContext;
import com.gee.spring.cloud.aerospace.simulate.core.satellite.telemetry.task.*;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import com.github.xiaoymin.knife4j.annotations.ApiSupport;
import io.netty.util.HashedWheelTimer;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.time.Instant;
import java.time.temporal.ChronoUnit;
import java.util.List;
import java.util.concurrent.TimeUnit;

/**
 * <p>
 * 卫星遥测帧配置 前端控制器
 * </p>
 *
 * @author gee
 * @since 2024-07-04
 */
@RestController
@Tag(name = "遥测", description = "遥测")
@ApiSupport(author = "gee", order = 1)
@RequestMapping("/mgt/satelliteTelemetry")
public class SatelliteTelemetryController {


    @Resource
    private TmManager tmManager;

    @GetMapping("/onCommand")
    @Operation(summary = "下发指令")
    @ApiOperationSupport(order = 1, author = "gee")
    public ApiResult<Boolean> onCommand(String command) {
        if ("start".equals(command)) {
            SatelliteContext satelliteContext = SatelliteContext.builder().tmSendIntervalMillis(1000).build();
            TmFrameTask tmFrameTask = new TmFrameTask(null);

            TmArcStartTask tmArcStartTask = new TmArcStartTask(null);

            TmArcEndTask tmArcEndTask = new TmArcEndTask(null);

            TmArcHandler tmArcHandler = new TmArcHandler(List.of(tmFrameTask, tmArcStartTask, tmArcEndTask));

            Instant now = Instant.now();

            SateTraceContext sateTraceContext = SateTraceContext.builder()
                    .devCode("TC1101")
                    .traceKey("2025051502")
                    .satelliteContext(satelliteContext)
                    .tmArcTimer(newHashedWheelTimer("tmArc"))
                    .tmFrameTimer(newHashedWheelTimer("tmFrame"))
                    .traceBeginTime(now.plusSeconds(10))
                    .traceEndTime(now.plus(2, ChronoUnit.MINUTES))
                    .tmArcHandler(tmArcHandler)
                    .tmArcStartTask(tmArcStartTask)
                    .tmArcEndTask(tmArcEndTask)
                    .tmFrameTask(tmFrameTask)
                    .build();
            System.out.println(sateTraceContext);
            return ApiResult.success(tmManager.start(sateTraceContext));
        }else if ("stop".equals(command)) {
            return ApiResult.success(tmManager.stop());
        }else if ("pause".equals(command)) {
            return ApiResult.success(tmManager.pause());
        }else{
            return ApiResult.success(tmManager.resume());
        }
    }

    private HashedWheelTimer newHashedWheelTimer(String threadName) {
        return new HashedWheelTimer(r -> new Thread(r,threadName), 1, TimeUnit.MILLISECONDS, 86400);
    }
}

